package toubiao.utils.old;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import toubiao.dao.business.ContactDaoI;
import toubiao.domain.po.business.PoContact;
import toubiao.domain.po.business.PoContract;
import toubiao.domain.po.business.PoPartA;
import toubiao.domain.po.business.PoProject;
import toubiao.domain.po.business.PoProjectSn;
import toubiao.domain.po.manage.PoResource;
import toubiao.domain.po.manage.PoRole;
import toubiao.domain.po.manage.PoUser;
import toubiao.domain.vo.business.ContractGrid;
import toubiao.domain.vo.business.ProjectGrid;
import toubiao.domain.vo.manage.Resource;
import toubiao.domain.vo.manage.User;
import toubiao.utils.ProConstant;
import toubiao.utils.ProUtilConfig;
import utils.UtilCollection;
import utils.UtilString;

public class ProUtilPoVoConvert {
	private static Logger logger=LoggerFactory.getLogger(ProUtilPoVoConvert.class);
	
	private static String divider=ProUtilConfig.getCollectionDividor();
	
	static public User getVo(PoUser poUser){
		if(poUser==null){
			return null;
		}
		User user=new User();
		BeanUtils.copyProperties(poUser, user);
		user.setRoleIds("");
		user.setRoleNames("");
		Set<PoRole> roleSet=poUser.getRoleSet();
		if(!UtilCollection.isNullOrEmpty(roleSet)){
			List<String> idList=new ArrayList<>();
			List<String> nameList=new ArrayList<>();
			for(PoRole role:roleSet){
				idList.add(role.getId()+"");
				nameList.add(role.getName());
			}
			user.setRoleIds(UtilCollection.Convert2String(idList, divider));
			user.setRoleNames(UtilCollection.Convert2String(nameList, divider));
		}
		return user;
	}
	
	static public Resource getVo(PoResource poResource){
		Resource resource=new Resource();
		BeanUtils.copyProperties(poResource, resource);
		return resource;
	}
	

	static public List<Resource> getVoListOfResource(List<PoResource> poList){
		List<Resource> voList=new ArrayList<Resource>();
		if(poList==null){
			return null;
		}
		for(PoResource poResource:poList){
			Resource resource=getVo(poResource);
			voList.add(resource);
		}
		return voList;
	}
	
	static public ProjectGrid getVo(PoProject project){
		if(project==null){
			return null;
		}
		ProjectGrid projectGrid=new ProjectGrid();
		BeanUtils.copyProperties(project, projectGrid);
		
		String contractIds="";
		String contractSns="";
		Double contractAmount=0D;
		
		Set<PoProjectSn> snSet=project.getSnSet();
		/*logger.info("id = "+project.getId());
		logger.info(snSet.size()+"");*/
		String sns="";
		
		List<Integer> snList=new ArrayList<>();
		if(!UtilCollection.isNullOrEmpty(snSet)){
			for(PoProjectSn sn:snSet){
				Integer integer=new Integer(-1);
				
				if(sn==null || sn.getSnInt()==null){
				}else {
					integer=sn.getSnInt();
				}
				snList.add(integer);
				logger.info(sns);
			}
			sns=UtilCollection.numberList2String(snList, "asc", ProConstant.STRING_DIVIDOR);
			logger.info(sns);
		}
		projectGrid.setSns(sns);
		
		Set<PoContract> contractSet=project.getContractSet();
		if(!UtilCollection.isNullOrEmpty(contractSet)){
			for(PoContract contract:contractSet){
				contractIds=contract.getId()+ProConstant.STRING_DIVIDOR;
				if(contract.getSn()==null){
					contractSns+=ProConstant.STRING_DIVIDOR;
				}else {
					contractSns=contract.getSn()+ProConstant.STRING_DIVIDOR;
				}
				contractAmount+=contract.getAmount();
			}
			contractIds=UtilString.deleteTail(contractIds, ProConstant.STRING_DIVIDOR.length());
			contractSns=UtilString.deleteTail(contractSns, ProConstant.STRING_DIVIDOR.length());
			
			projectGrid.setContractIds(contractIds);
			projectGrid.setContractSns(contractSns);
			projectGrid.setContractAmount(contractAmount);
		}
		
		PoPartA partA=project.getPartA();
		if(partA!=null){
			projectGrid.setPartAId(partA.getId());
			projectGrid.setPartAName(partA.getName());
			projectGrid.setPartAType(partA.getType());
		}

		return projectGrid;
	}
	
	static public List<ProjectGrid> getVoListOfProjectGrid(List<PoProject> projectList){
		if(projectList==null){
			return null;
		}
		List<ProjectGrid> gridList=new ArrayList<>();
		for(PoProject poProject:projectList){
			ProjectGrid projectGrid=getVo(poProject);
			gridList.add(projectGrid);
		}
		return gridList;
	}
	
	static public ContractGrid getVoOfContractGrid(PoContract poContract){
		if(poContract==null){
			return null;
		}
		ContractGrid contractGrid=new ContractGrid();
		
		BeanUtils.copyProperties(poContract, contractGrid);
		
		Set<PoProject> projectSet=poContract.getProjectSet();
		if(!UtilCollection.isNullOrEmpty(projectSet)){
			StringBuilder projectIds=new StringBuilder();
			StringBuilder projectSns=new StringBuilder();
			Double projectInvestAmount=0D;
			for(PoProject poProject:projectSet){
				projectIds.append(poProject.getId()).append(ProConstant.STRING_DIVIDOR);
				
				if(!UtilCollection.isNullOrEmpty(poProject.getSnSet())){
					Set<PoProjectSn> snSet=poProject.getSnSet();
					StringBuilder sns=new StringBuilder();
					for(PoProjectSn sn:snSet){
						if(sn.getSnInt()==null){
							sns.append(ProConstant.STRING_DIVIDOR);
						}else {
							sns.append(sn.getSnInt()+ProConstant.STRING_DIVIDOR);
						}
					}
					String snsString=UtilString.deleteTail(sns.toString(), ProConstant.STRING_DIVIDOR.length());
					projectSns.append(snsString).append(";");
				}else {
					projectSns.append(";");
				}
				
				if(poProject.getInvestAmount()!=null){
					projectInvestAmount+=poProject.getInvestAmount();
				}
			}
			
			contractGrid.setProjectIds(UtilString.deleteTail(projectIds.toString(), ProConstant.STRING_DIVIDOR.length()));
			contractGrid.setProjectSns(UtilString.deleteTail(projectSns.toString(), ";".length()));
			contractGrid.setProjectInvestAmount(projectInvestAmount);
		}
		
		if(poContract.getEntrustPartA()!=null){
			PoPartA poPartA=poContract.getEntrustPartA();
			contractGrid.setPartAId(poPartA.getId());
			contractGrid.setPartAName(poPartA.getName());
			contractGrid.setPartAType(poPartA.getType());
		}
		
		Set<PoContact> contactSet=poContract.getContactSet();
		if(!UtilCollection.isNullOrEmpty(contactSet)){
			StringBuilder contactIdsSb=new StringBuilder();
			StringBuilder contactNameAndPhoneSb=new StringBuilder();
			for(PoContact poContact:contactSet){
				contactIdsSb.append(poContact.getId()).append(ProConstant.STRING_DIVIDOR);
				if(poContact.getName()==null){
					contactNameAndPhoneSb.append("xxx");
				}else {
					contactNameAndPhoneSb.append(poContact.getName());
				}
				contactNameAndPhoneSb.append(":");
				
				if(poContact.getPhone()==null){
					contactNameAndPhoneSb.append("xxx");
				}else {
					contactNameAndPhoneSb.append(poContact.getPhone());
				}
				
				
				contactNameAndPhoneSb.append(ProConstant.STRING_DIVIDOR);
			}
			contractGrid.setContactNamesAndPhones(UtilString.deleteTail(contactNameAndPhoneSb.toString(), ProConstant.STRING_DIVIDOR.length()));
		}
		
		return contractGrid;
	}
	
	
	static public List<ContractGrid> getVoListOfContractGrid(List<PoContract> poContractList){
		if(poContractList==null){
			return null;
		}
		List<ContractGrid> contractGridList=new ArrayList<>();
		for(PoContract contract:poContractList){
			ContractGrid contractGrid=getVoOfContractGrid(contract);
			contractGridList.add(contractGrid);
		}
		return contractGridList;
	}
}
